#pragma once
#include "stdafx.h"
#include "aps.h"
#include "dgdef.h"
#include <fstream>

using namespace efsod;
using namespace std;
using namespace aps;

namespace Staff1 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Data::Odbc;
	using namespace System::IO;
	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  go;
	protected: 

	protected: 

	private: System::Windows::Forms::Label^  newPass;

	private: System::Windows::Forms::Button^  write;
	private: System::Windows::Forms::GroupBox^  passInfo;
	private: System::Windows::Forms::Label^  pNo1;

	private: System::Windows::Forms::Label^  fNo1;


	private: System::Windows::Forms::PictureBox^  faceImage;
	private: System::Windows::Forms::Label^  pNo2;

	private: System::Windows::Forms::Label^  fNo2;
	public: System::Windows::Forms::RichTextBox^  progress;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  csn;
	private: System::Windows::Forms::PictureBox^  alert;
	private: System::Windows::Forms::ComboBox^  newPassNo;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->go = (gcnew System::Windows::Forms::Button());
			this->newPass = (gcnew System::Windows::Forms::Label());
			this->write = (gcnew System::Windows::Forms::Button());
			this->passInfo = (gcnew System::Windows::Forms::GroupBox());
			this->pNo2 = (gcnew System::Windows::Forms::Label());
			this->fNo2 = (gcnew System::Windows::Forms::Label());
			this->pNo1 = (gcnew System::Windows::Forms::Label());
			this->fNo1 = (gcnew System::Windows::Forms::Label());
			this->faceImage = (gcnew System::Windows::Forms::PictureBox());
			this->progress = (gcnew System::Windows::Forms::RichTextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->csn = (gcnew System::Windows::Forms::Label());
			this->alert = (gcnew System::Windows::Forms::PictureBox());
			this->newPassNo = (gcnew System::Windows::Forms::ComboBox());
			this->passInfo->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->faceImage))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->alert))->BeginInit();
			this->SuspendLayout();
			// 
			// go
			// 
			this->go->Location = System::Drawing::Point(330, 23);
			this->go->Name = L"go";
			this->go->Size = System::Drawing::Size(32, 23);
			this->go->TabIndex = 0;
			this->go->Text = L"Go";
			this->go->UseVisualStyleBackColor = true;
			this->go->Click += gcnew System::EventHandler(this, &Form1::go_Click);
			// 
			// newPass
			// 
			this->newPass->AutoSize = true;
			this->newPass->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->newPass->Location = System::Drawing::Point(17, 26);
			this->newPass->Name = L"newPass";
			this->newPass->Size = System::Drawing::Size(154, 15);
			this->newPass->TabIndex = 2;
			this->newPass->Text = L" New Passport Number";
			// 
			// write
			// 
			this->write->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->write->Location = System::Drawing::Point(419, 189);
			this->write->Name = L"write";
			this->write->Size = System::Drawing::Size(131, 23);
			this->write->TabIndex = 4;
			this->write->Text = L"Write files on card";
			this->write->UseVisualStyleBackColor = true;
			this->write->Click += gcnew System::EventHandler(this, &Form1::write_Click);
			// 
			// passInfo
			// 
			this->passInfo->Controls->Add(this->pNo2);
			this->passInfo->Controls->Add(this->fNo2);
			this->passInfo->Controls->Add(this->pNo1);
			this->passInfo->Controls->Add(this->fNo1);
			this->passInfo->Controls->Add(this->faceImage);
			this->passInfo->Location = System::Drawing::Point(22, 52);
			this->passInfo->Name = L"passInfo";
			this->passInfo->Size = System::Drawing::Size(528, 122);
			this->passInfo->TabIndex = 5;
			this->passInfo->TabStop = false;
			this->passInfo->Text = L"Passport Details";
			// 
			// pNo2
			// 
			this->pNo2->AutoSize = true;
			this->pNo2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->pNo2->Location = System::Drawing::Point(186, 61);
			this->pNo2->Name = L"pNo2";
			this->pNo2->Size = System::Drawing::Size(78, 15);
			this->pNo2->TabIndex = 4;
			this->pNo2->Text = L"Not Available";
			// 
			// fNo2
			// 
			this->fNo2->AutoSize = true;
			this->fNo2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->fNo2->Location = System::Drawing::Point(186, 30);
			this->fNo2->Name = L"fNo2";
			this->fNo2->Size = System::Drawing::Size(78, 15);
			this->fNo2->TabIndex = 3;
			this->fNo2->Text = L"Not Available";
			// 
			// pNo1
			// 
			this->pNo1->AutoSize = true;
			this->pNo1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->pNo1->Location = System::Drawing::Point(35, 61);
			this->pNo1->Name = L"pNo1";
			this->pNo1->Size = System::Drawing::Size(118, 15);
			this->pNo1->TabIndex = 2;
			this->pNo1->Text = L"Passport Number";
			// 
			// fNo1
			// 
			this->fNo1->AutoSize = true;
			this->fNo1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->fNo1->Location = System::Drawing::Point(35, 27);
			this->fNo1->Name = L"fNo1";
			this->fNo1->Size = System::Drawing::Size(86, 15);
			this->fNo1->TabIndex = 1;
			this->fNo1->Text = L"File Number";
			// 
			// faceImage
			// 
			this->faceImage->Location = System::Drawing::Point(397, 21);
			this->faceImage->Name = L"faceImage";
			this->faceImage->Size = System::Drawing::Size(85, 76);
			this->faceImage->TabIndex = 0;
			this->faceImage->TabStop = false;
			// 
			// progress
			// 
			this->progress->Location = System::Drawing::Point(22, 270);
			this->progress->Name = L"progress";
			this->progress->Size = System::Drawing::Size(528, 76);
			this->progress->TabIndex = 6;
			this->progress->Text = L"";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label1->Location = System::Drawing::Point(19, 243);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(64, 15);
			this->label1->TabIndex = 5;
			this->label1->Text = L"Progress";
			// 
			// csn
			// 
			this->csn->AutoSize = true;
			this->csn->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->csn->Location = System::Drawing::Point(57, 220);
			this->csn->Name = L"csn";
			this->csn->Size = System::Drawing::Size(115, 15);
			this->csn->TabIndex = 7;
			this->csn->Text = L"Chip Serial Number";
			// 
			// alert
			// 
			this->alert->Location = System::Drawing::Point(27, 209);
			this->alert->Name = L"alert";
			this->alert->Size = System::Drawing::Size(24, 24);
			this->alert->TabIndex = 8;
			this->alert->TabStop = false;
			// 
			// newPassNo
			// 
			this->newPassNo->FormattingEnabled = true;
			this->newPassNo->Location = System::Drawing::Point(193, 25);
			this->newPassNo->Name = L"newPassNo";
			this->newPassNo->Size = System::Drawing::Size(121, 21);
			this->newPassNo->TabIndex = 9;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(574, 358);
			this->Controls->Add(this->newPassNo);
			this->Controls->Add(this->alert);
			this->Controls->Add(this->csn);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->progress);
			this->Controls->Add(this->passInfo);
			this->Controls->Add(this->write);
			this->Controls->Add(this->newPass);
			this->Controls->Add(this->go);
			this->Name = L"Form1";
			this->Text = L"Passport Creation";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->passInfo->ResumeLayout(false);
			this->passInfo->PerformLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->faceImage))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->alert))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				 System::String ^str = "dsn=Passport Data;uid=siddhu;pwd=siddhu;";
				 System::Data::Odbc::OdbcConnection ^connection = gcnew System::Data::Odbc::OdbcConnection(str);
				 System::Data::Odbc::OdbcCommand ^command = connection->CreateCommand();
				 System::Data::Odbc::OdbcDataReader ^Reader;
				 command->CommandText = "SELECT passport_no FROM pdata where passport_no IS NOT NULL and chip_serial_no IS NULL";
				 connection->Open();
				 Reader = command->ExecuteReader();
				 while (Reader->Read())
					this->newPassNo->Items->Add(Reader->GetValue(0)->ToString());
				 connection->Close();
			 }
	
	private: System::Void write_Click(System::Object^  sender, System::EventArgs^  e) {
				//TODO:
				//2. Reads chip serial number from card and update in database
				System::String ^str = "dsn=Passport Data;uid=siddhu;pwd=siddhu;";
				System::Data::Odbc::OdbcConnection ^connection = gcnew System::Data::Odbc::OdbcConnection(str);
				System::Data::Odbc::OdbcCommand ^command = connection->CreateCommand();
				System::Data::Odbc::OdbcDataReader ^Reader;
				command->CommandText = "SELECT * FROM pdata WHERE passport_no = '"+ this->newPassNo->SelectedItem->ToString()+"'";
				connection->Open();
				Reader = command->ExecuteReader();
				Reader->Read();
				//System::String ^s = Reader->GetValue(Reader->GetOrdinal("file_number"))->ToString();
				System::String ^s = this->fNo2->Text;
				
				System::IO::FileStream ^fs;
				int FileSize = 65 * 1024;
				array<System::Byte, 1> ^rawData;
				rawData = gcnew array<System::Byte, 1>(FileSize);
				rawData->Initialize();
				Reader->GetBytes(Reader->GetOrdinal("portrait_image"), 0, rawData, 0, FileSize);
				fs = gcnew System::IO::FileStream("FaceImage.jpg", FileMode::OpenOrCreate, FileAccess::Write);
				fs->Write(rawData, 0, FileSize);
				fs->Close();
				connection->Close();

				command->CommandText = "UPDATE pdata set personalization_timestamp = now() where file_number = '" + s + "'";
				connection->Open();
				Reader = command->ExecuteReader();
				Reader->Read();
				connection->Close();

				char* fileno = (char*)malloc(20);
				array<wchar_t, 1> ^c;
				c = gcnew array<wchar_t, 1>(s->Length);
				
				c->Initialize();
				c = s->ToCharArray();
				for(int i=0;i<s->Length;i++) fileno[i] = c[i];
				fileno[s->Length] = '\0';
				
				BYTE d1[100];
				BYTE d2[70000];
				BYTE d11[500];
				BYTE d12[200];
				BYTE d13[500];
				BYTE COM[500];

				this->progress->AppendText("Processing started for passport number " + this->newPassNo->SelectedItem->ToString() + "\r");
				try {
				int len1 = aps::createDG1(fileno, d1, 100);
				this->progress->AppendText("DG1 created\r" );
				ofstream fout1("DG1.bin", ios::binary);
				fout1.write((char*)d1, len1);

				int len2 = aps::createDG2(fileno, d2, 70000);
				this->progress->AppendText("DG2 created\r");
				ofstream fout2("DG2.bin", ios::binary);
				fout2.write((char*)d2, len2);

				/*int len12 = aps::createDG12(fileno, "doc_signer.der", d12, 200);
				this->progress->AppendText("DG12 created\r");
				ofstream fout12("DG12.bin", ios::binary);
				fout12.write((char*)d12, len12);*/
				
				int len13 = aps::createDG13(fileno, d13, 500);
				this->progress->AppendText("DG13 created\r");
				ofstream fout13("DG13.bin", ios::binary);
				fout13.write((char*)d13, len13);

				int len11 = aps::createDG11(fileno, d11, 500);
				this->progress->AppendText("DG11 created\r");
				ofstream fout11("DG11.bin", ios::binary);
				fout11.write((char*)d11, len11);

				int clen = efsod::createEFCOM(COM, 100);
				this->progress->AppendText("EFCOM created\r");
				ofstream foutc("EF_COM.bin", ios::binary);
				foutc.write((char*)COM, clen);

				this->progress->AppendText("Processing done for passport number " + this->newPassNo->SelectedItem->ToString() + "\r");
				}
				catch (System::Exception ^excp) {
					this->progress->AppendText("Kindly execute again\r");
					return ;
				}
				//Read CSN from card and update in database.
				int csn = 101;
				command->CommandText = "UPDATE pdata set chip_serial_no = '" + csn.ToString() + "' where file_number ='" + s + "'";
				connection->Open();
				Reader = command->ExecuteReader();
				Reader->Read();
				connection->Close();
				}

private: System::Void go_Click(System::Object^  sender, System::EventArgs^  e) {
				System::String ^str = "dsn=Passport Data;uid=siddhu;pwd=siddhu;";
				System::Data::Odbc::OdbcConnection ^connection = gcnew System::Data::Odbc::OdbcConnection(str);
				System::Data::Odbc::OdbcCommand ^command = connection->CreateCommand();
				System::Data::Odbc::OdbcDataReader ^Reader;
				command->CommandText = System::String::Concat("SELECT file_number, passport_no FROM pdata WHERE passport_no = '", this->newPassNo->SelectedItem->ToString(), "'");
				connection->Open();
				Reader = command->ExecuteReader();
				Reader->Read();
				this->fNo2->Text = Reader->GetValue(0)->ToString();
				this->pNo2->Text = Reader->GetValue(1)->ToString();
				connection->Close();
		 }
};
}